﻿<template>
	<a-card :bordered="false" style="margin-bottom: 10px" v-if="!configShow">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item name="code" label="唯一编码">
						<a-input v-model:value="searchFormState.code" placeholder="请输入唯一编码"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item name="configId" label="所属库">
						<a-input v-model:value="searchFormState.configId" placeholder="请输入所属库"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item name="entityName" label="实体名">
						<a-input v-model:value="searchFormState.entityName" placeholder="请输入实体名"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item name="tableName" label="表名">
						<a-input v-model:value="searchFormState.tableName" placeholder="请输入表名"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="table.refresh(true)">
						<template #icon><SearchOutlined /></template>查询</a-button
					>
					<a-button class="buttom-left" @click="() => searchFormRef.resetFields()">
						<template #icon><redo-outlined /></template>重置</a-button
					>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" v-if="!configShow">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			:tool-config="toolConfig"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon><plus-outlined /></template>新增配置</a-button
					>
					<batch-delete
						title="确定要删除选中表吗?"
						buttonName="删除配置"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchBatchEdit"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a @click="openConfig(record)">配置</a>
					<a-divider type="vertical" />
					<a @click="SyncConfig(record)">同步</a>
					<a-divider type="vertical" />
					<a-popconfirm title="确定要删除吗？" @confirm="deleteBatchEdit(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<Config ref="configRef" @successful="table.refresh(true)" @closed="configShow = false" />
</template>

<script setup name="batch">
	import { message, Empty } from 'ant-design-vue'
	import Form from './form.vue'
	import Config from './config.vue'
	import batchEditApi from '@/api/sys/batchEditApi'

	let searchFormState = reactive({}) //查询表单状态
	const searchFormRef = ref() //查询表单
	const table = ref() //表格
	const formRef = ref() //新增表单页
	const configShow = ref(false) //显示配置页面
	const configRef = ref() //配置表单页
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false } //表格工具配置

	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '唯一编码',
			dataIndex: 'code'
		},
		{
			title: '所属库',
			dataIndex: 'configId'
		},
		{
			title: '实体名',
			dataIndex: 'entityName'
		},
		{
			title: '表名',
			dataIndex: 'tableName'
		},
		{
			title: '表描述',
			dataIndex: 'tableDescription'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '200px'
		}
	]

	//选择项
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = reactive({
		alert: {
			show: false, //显示选择多少个
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	})

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return batchEditApi.batchEditPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}

	// 删除
	const deleteBatchEdit = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		batchEditApi.batchEditDelete(params).then(() => {
			table.value.refresh(true)
		})
	}

	// 批量删除
	const deleteBatchBatchEdit = (params, callback) => {
		batchEditApi
			.batchEditDelete(params)
			.then(() => {})
			.finally(() => {
				table.value.clearRefreshSelected()
				callback() //调用组件中的回调方法
			})
	}

	// 配置
	const openConfig = (record) => {
		configShow.value = true
		configRef.value.onOpen(record)
	}

	// 同步
	const SyncConfig = (record) => {
		batchEditApi.SyncColumns({ id: record.id }).then(() => {
			message.success('同步成功')
		})
	}
</script>

<style scoped>
	.buttom-left {
		margin-left: 8px;
	}
</style>
